package com.bonc.modules.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.bonc.common.utils.*;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.modules.business.entity.TbCommonParamInfoEntity;
import com.bonc.modules.business.entity.TbWorkOrderArchiveEntity;
import com.bonc.modules.business.entity.TbWorkOrderInfoEntity;
import com.bonc.modules.business.service.TbWorkOrderInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bonc.modules.business.dao.TbWorkOrderCancelDao;
import com.bonc.modules.business.entity.TbWorkOrderCancelEntity;
import com.bonc.modules.business.service.TbWorkOrderCancelService;

@DataSource("data")
@Service("tbWorkOrderCancelService")
public class TbWorkOrderCancelServiceImpl extends ServiceImpl<TbWorkOrderCancelDao, TbWorkOrderCancelEntity> implements TbWorkOrderCancelService {

    @Autowired
    private TbWorkOrderCancelService tbWorkOrderCancelService;

    @Autowired
    private TbWorkOrderInfoService tbWorkOrderInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String identifier = (String)params.get("identifier");
        IPage<TbWorkOrderCancelEntity> page = this.page(
                new Query<TbWorkOrderCancelEntity>().getPage(params),
                new QueryWrapper<TbWorkOrderCancelEntity>().like(StringUtils.isNotBlank(identifier),"identifier", identifier)
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(TbWorkOrderCancelEntity tbWorkOrderCancel) {
        return SqlHelper.retBool(this.getBaseMapper().insert(tbWorkOrderCancel));
    }

    @Override
    public List<Map<String, Object>> queryCancelList() {
        return this.baseMapper.queryCancelList();
    }

    @Override
    public AtomicReference<String> postOrderCancelService(TbWorkOrderCancelEntity orderCancel) {
        /*LambdaQueryWrapper<TbWorkOrderCancelEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<TbWorkOrderCancelEntity> list = tbWorkOrderCancelService.list(queryWrapper);*/

        AtomicReference<String> res = new AtomicReference<>("");
        AtomicReference<String> req = new AtomicReference<>("");

        /*list.forEach(orderCancel->{

        });*/

        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        orderCancel.setWithdrawTime(dateFormat.format(calendar.getTime()));

        String url = "http://10.254.30.252:3900/api/v2/sync/CSVC/WithdrawCSS";
        SupermanUtil util = new SupermanUtil();

        JSONObject content = new JSONObject();
        content.put("Identifier", orderCancel.getIdentifier());
        content.put("WithdrawTime", orderCancel.getWithdrawTime());
        content.put("WithdrawReason", orderCancel.getWithdrawReason());

        Map<String, Object> param = ResolveParamUtils.getAllParam(content);

        req.set(JSON.toJSONString(param));
        res.set(util.sendPost(url, req.get()));

        TbWorkOrderInfoEntity tbWorkOrderInfo = new TbWorkOrderInfoEntity();
        tbWorkOrderInfo.setIdentifier(orderCancel.getIdentifier());
        tbWorkOrderInfo.setState("03");
        tbWorkOrderInfo.setHandingStatus("0");
        tbWorkOrderInfoService.updateById(tbWorkOrderInfo);

        tbWorkOrderCancelService.save(orderCancel);

        return res;
    }

    @Override
    public ReplyCommon WithdrawCSS(TbCommonParamInfoEntity tbCommonParamValue) {
        String content = "";
        String rspCode = "";
        String rspDesc = "";

        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat1= new SimpleDateFormat("yyyyMMddHHmmss");
        String cutOffDay = dateFormat.format(calendar.getTime());
        String handingTime = dateFormat1.format(calendar.getTime());
        String transIDO = handingTime + System.currentTimeMillis();
        String providePartyID = "CSVB3510";

        /*
        String signjy = ResolveParamUtils.analysisAllParam(tbCommonParamValue);
        if(sign == signjy || sign.equals(signjy)){

        }else{
            rspCode = "20000";
            rspDesc = "非法的签名";
        }*/
        content = tbCommonParamValue.getContent();
        TbWorkOrderCancelEntity tbWorkOrderCancel = JSONObject.parseObject(content,TbWorkOrderCancelEntity.class);
        if(StringUtils.isBlank(tbWorkOrderCancel.getIdentifier()) || StringUtils.isBlank(tbWorkOrderCancel.getWithdrawReason()) ||
                StringUtils.isBlank(tbWorkOrderCancel.getWithdrawTime())){
            rspCode = "20101";
            rspDesc = "缺少必须的参数";
        }else{
            //通过传入的工单编号 查询此工单的状态 如果为处理中 才可以撤回
            TbWorkOrderInfoEntity tbWorkOrderInfo = tbWorkOrderInfoService.getById(tbWorkOrderCancel.getIdentifier());
            if(tbWorkOrderInfo == null){
                rspCode = "20000";
                rspDesc = "此工单不存在，请检查！";
            }else{
                System.out.println(tbWorkOrderInfo.getState());
                if(tbWorkOrderInfo.getState().equals("01") || tbWorkOrderInfo.getState().equals("06")){
                    //工单状态满足撤回情形 处理单和催办单均可以撤回 将工单状态更改为撤回状态 并将操作入库
                    tbWorkOrderInfo.setState("03");
                    tbWorkOrderInfo.setHandingStatus("0");
                    tbWorkOrderInfoService.updateById(tbWorkOrderInfo);
                    boolean num = tbWorkOrderCancelService.save(tbWorkOrderCancel);//将工单撤回信息存入工单撤回码表
                    if(num == true){
                        rspCode = "00000";
                        rspDesc = "success";
                    }else{
                        rspCode = "20000";
                        rspDesc = "基础服务系统内部错误";
                    }
                }else if(tbWorkOrderInfo.getState().equals("03")){
                    rspCode = "20000";
                    rspDesc = "当前工单已撤回";
                }else{
                    rspCode = "20000";
                    rspDesc = "当前工单状态不得撤回";
                }
            }
        }
        return ReplyCommon.ok(transIDO,cutOffDay,providePartyID,rspCode,rspDesc);
    }

}
